import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.ibm.mqtt.IMqttClient;
import com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttNotConnectedException;
import com.ibm.mqtt.MqttPersistence;
import com.ibm.mqtt.MqttClient;
import com.ibm.mqtt.MqttPersistenceException;
import com.ibm.mqtt.MqttSimpleCallback;


public class With_parser implements MqttSimpleCallback{

	//a unique client-id must( must be unique to the message broker)
	public static String CLIENT_ID="dalelane.mqtt";

	static //implements the connection to the message broker
	IMqttClient mqttClient=null;

	//host name of the server we're receiving push notifications from
    private static String          brokerHostName       = "127.0.0.1";
	
	private static int BROKER_PORT_NUM=1883;
	private static MqttPersistence PERSISTENCE=null;
	private static boolean CLEAN_START=true;
	private static short KEEP_ALIVE=5;
	private static int QUALITY_OF_SERVICE=0;
	private static int[] QUALITIES_OF_SERVICE={0};
	private static boolean RETAINED_PUBLISH=false;
	
	public static void main(String[] args) throws ParserException{	
		With_parser obj=new With_parser();
		obj.connectToBroker();
	    obj.subscribeToTopic("/iisc/cs");
				
		//we need to take care of how often we publish
		//meaning the below should be in a loop, within the loop
		//needs to be the parser code
		obj.publishToTopic("/iisc/it");
	}

	/*creates a connection to the specified message broker*/

	private void connectToBroker()  {
		
		System.out.println("in connect");
		String mqttConnSpec="tcp://"+brokerHostName+"@"+BROKER_PORT_NUM;

		try {
			mqttClient=MqttClient.createMqttClient(mqttConnSpec,PERSISTENCE);
			mqttClient.connect(CLIENT_ID,CLEAN_START,KEEP_ALIVE);
		}
		catch(MqttException e){
			e.printStackTrace();
		}		
		mqttClient.registerSimpleHandler(this);
	}
	private void subscribeToTopic(String topicname)
	{
		System.out.println("in subscribe");
		try{
			String[] topics={topicname};
			mqttClient.subscribe(topics, QUALITIES_OF_SERVICE);
		}
		catch(MqttNotConnectedException e)
		{
			e.printStackTrace();
		}
		catch(IllegalArgumentException e)
		{
			e.printStackTrace();
		}
		catch(MqttException e)
		{
			e.printStackTrace();
		}
	}
	public void publishArrived(String topicname,byte[] payload,int qos,boolean retained)
	{
		String value=new String(payload);
		System.out.println(value);
	}
	//specifies the topic and the message it has, to publish
	private void publishToTopic(String topicname) throws ParserException
	{
		Parser parser;
		NodeFilter filter;
		NodeList list;
		String msg="hi";
		

		filter = new HasAttributeFilter ("id", "tvvscroll");
		parser = new Parser ("http://www.nitk.ac.in");
		list = parser.extractAllNodesThatMatch (filter);
		
		for (int i = 0; i <=0; i++)
			msg=msg+list.elementAt (i).toPlainTextString ();
		
		
		System.out.println("in publish");
		try{
			mqttClient.publish(topicname, msg.getBytes(),QUALITY_OF_SERVICE,RETAINED_PUBLISH);
		}
		catch(MqttNotConnectedException e){
			e.printStackTrace();
		}
		catch(MqttPersistenceException e){
			e.printStackTrace();
		}
		catch(IllegalArgumentException e){
			e.printStackTrace();
		}
		catch(MqttException e){
			e.printStackTrace();
		}
	}

	@Override
	public void connectionLost() throws Exception {
		// TODO Auto-generated method stub
		
	}
}	